package cn.jluzh.blog.modules.security.service.impl;

import cn.jluzh.blog.commons.result.Result;
import cn.jluzh.blog.commons.result.ResultFactory;
import cn.jluzh.blog.mapper.SysUserTokenMapper;
import cn.jluzh.blog.modules.security.eneity.SysUserToken;
import cn.jluzh.blog.modules.security.ouath2.TokenGenerator;
import cn.jluzh.blog.modules.security.service.SysUserTokenService;
import cn.jluzh.blog.modules.user.service.impl.UserServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author hzhii
 */
@Service
public class SysUserTokenServiceImpl implements SysUserTokenService {

    @Autowired
    SysUserTokenMapper sysUserTokenMapper;

    @Autowired
    UserServiceImpl userService;

    /**
     * 12小时后过期
     */
    private final static int EXPIRE = 3600 * 12;

    @Override
    public Result createToken(Integer userId) {

        //生成token
        String token;

        //当前时间
        Date now = new Date();

        //过期时间
        Date expireTime = new Date(now.getTime() + EXPIRE * 1000);

        //判断是否生成过token
        SysUserToken sysToken = this.getTokenByUserId(userId);
        if(sysToken == null){
            token = TokenGenerator.generateValue();
            sysToken = new SysUserToken();
            sysToken.setUserId(userId);
            sysToken.setUpdateDate(now);
            sysToken.setExpireDate(expireTime);
            sysToken.setToken(token);

            sysUserTokenMapper.insert(sysToken);
        }else{
            //判断token是否过期
            if(sysToken.getExpireDate().getTime() < System.currentTimeMillis()){
                //token过期，重新生成token
                token = TokenGenerator.generateValue();
            }else {
                token = sysToken.getToken();
            }
            sysToken.setToken(token);
            sysToken.setUpdateDate(now);
            sysToken.setExpireDate(expireTime);

            //更新token
            sysUserTokenMapper.updateById(sysToken);
        }

        JSONObject object = new JSONObject();
        object.put("Oauth-Token", token);
        object.put("expire", EXPIRE);

        return ResultFactory.buildSuccessResult(object);

    }

    @Override
    public void logout(SysUserToken token) {
        //退出更新数据库token
        sysUserTokenMapper.deleteById(token);
    }

    @Override
    public SysUserToken getTokenByUserId(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        SysUserToken sysUserToken = sysUserTokenMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(sysUserToken)){
            return null;
        }else {
            return sysUserToken;
        }
    }

    @Override
    public SysUserToken getByToken(String token) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("token",token);
        SysUserToken token1 = sysUserTokenMapper.selectOne(queryWrapper);
        return token1;
    }
}
